/* 
 * File:   RpcDeliveryClient.cpp
 * Author: bd
 * 
 * Created on April 2, 2011, 6:32 PM
 */

#include "RpcDeliveryClient.hpp"
#include "inc.hpp"

const std::string RpcDeliveryClient::DELIVER_SM = "<deliver_sm>";
const std::string RpcDeliveryClient::DELIVER_SM_ = "</deliver_sm>";

RpcDeliveryClient::RpcDeliveryClient(const std::string& connStr) :
DeliveryClient(connStr)
{
}

RpcDeliveryClient::~RpcDeliveryClient()
{
}

void RpcDeliveryClient::connect()
{
    size_t tmp = targetUrl_.find(':');
    if (tmp == std::string::npos)
        return;
    std::string addr = targetUrl_.substr(0, tmp);
    std::string port = targetUrl_.substr(tmp + 1);
    LOG(INFO) << "[RPC] Connecting..." << "[" << addr << ":" << port << "]" << "\n";
    socket_ = boost::shared_ptr<tcp::iostream > (new tcp::iostream(addr, port));
    connected_ = true;
    LOG(INFO) << "[RPC] Connecting..." << "done" << "\n";
}

void RpcDeliveryClient::send(const ShortMsgItem& smi)
{
    LOG(INFO) << "Sending... [" << smi << "]" << "\n";
    *socket_ << "<?xml version = \"1.0\" ?>";
    *socket_ << DELIVER_SM;
    serializeXml(*socket_, smi);
    *socket_ << DELIVER_SM_ << "\n";
    LOG(INFO) << "Sending..." << "done";
}
